package it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.eclipse.emf.ecore.util.EcoreUtil;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.EmacsManager;
import it.univaq.mancoosi.injectors.systemconfiguration.managers.IconCacheManager;
import it.univaq.mancoosi.injectors.systemconfiguration.utils.ProcessCommand;
import it.univaq.mancoosi.mancoosimm.BootScript;
import it.univaq.mancoosi.mancoosimm.File;

public class UbuntuEmacsManager extends EmacsManager {
	
	String emacsPath = "/usr/lib/emacsen-common/";
//	String emacsPath = "/home/martina/Desktop/emacsen-common/";
	protected int pkgNumber = 0;
	
	public UbuntuEmacsManager() {
		super();
	}

	public static EmacsManager getInstance() {
		if (INSTANCE == null)
			INSTANCE = new UbuntuEmacsManager();
		return INSTANCE;
	}

	@Override
	public void createEmacsPackageFromSystem() {		
		String line;		
		syncFilesMap();
		
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getEmacspkg() != null){
			this.emacsPackage.getFiles().addAll(UbuntuEnvironmentManager.getInstance().getEnvironment().getEmacspkg().getFiles());
		}
		try {
			String[] cmd = {"/bin/sh","-c","ls --time-style=long-iso " + emacsPath};
			BufferedReader input = ProcessCommand.getInstance().getResult(cmd);	

			while ((line = input.readLine()) != null) {

				java.io.File file = new java.io.File(emacsPath + line);
				
				if(file.exists()){
					pkgNumber++;
					File emacsModelFile = null;
					if (!existsFile(file.getPath())) {
						emacsModelFile = UbuntuFileSystemManager.getInstance()
						.createFile(file.getPath());
						this.emacsPackage.getFiles().add(emacsModelFile);
						this.filesMap.put(file.getPath(), emacsModelFile);
					}												
				}
			}
			
			this.emacsPackage.setEnv(UbuntuEnvironmentManager.getInstance().getEnvironment());
			
			input.close();

		} catch (Exception err) {
			err.printStackTrace();
		}

	}

	@Override
	public void synchronize() throws IOException {
		createEmacsPackageFromSystem();
		
		//check whether there have been changes in the number of alternatives
//		int numPkgInTheModel = 0;
//		if(UbuntuEnvironmentManager.getInstance().getEnvironment() != null){
//			numPkgInTheModel = UbuntuEnvironmentManager.getInstance().getEnvironment().getEmacspkg().getFiles().size();
//		}
//		int numAddedPkg = addedpkgAfterSync;

		int pkgInTheSystem = pkgNumber;
		
		if(!((this.filesMap.size()/*numPkgInTheModel + numAddedPkg*/) == pkgInTheSystem)){		
			Iterator<File> iterator = this.filesMap.values().iterator();
			while (iterator.hasNext()) {
				File curr = iterator.next();
				if (isMissingFileInTheSystem(curr.getLocation())) {
					this.filesMap.remove(curr);
					EcoreUtil.delete(curr);
					UbuntuFileSystemManager.getInstance().deleteFile(curr.getLocation());
				}
			}
		}
	}
	
	public boolean isMissingFileInTheSystem (String fileLocation) {
		if(UbuntuFileSystemManager.getInstance().getFile(fileLocation) != null){
			return UbuntuFileSystemManager.getInstance().getFile(fileLocation).isIsMissing();
		}else{
			return true;
		}
	}

	@Override
	public Boolean existsFile(String fileLocation) {
		return this.filesMap.containsKey(fileLocation);
	}

}
